<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class patient extends MY_Controller {

   
    public function __construct() {
        parent::__construct();
        $this->per_page = 1;
    }

    public function index($page = false) {
        // /*         * pagination */
        // $row = $this->general_model->get('patients', '', '', '', '', 'count');
        // $url = base_url() . 'patient/index/';
        // parent::pagination($page, $url, $row, $this->per_page, 3);
        // /*         * pagination */
        // $data['patient'] = $this->general_model->get('patients', $this->start_no, $this->per_page, 'date', $group_by = false, '');
        // //echo $this->db->last_query();die();
        // $view = 'core_' . $this->usertype . '_view';
        // parent::$view('list_pat', $data);
    }

    public function profile($id) {

        $data['patient'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['pat_adr1'] = $this->general_model->get_row('adress', "pat_id = {$id} and ads_type ='1'");
        $data['pat_adr2'] = $this->general_model->get_row('adress', "pat_id = {$id} and ads_type ='2'");

        //echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/profile', $data);
    }

    public function list_pat($page = false) {
        /*         * pagination */
        $this->per_page = 20;
        $row = $this->general_model->get('patients', '', '', '', '', 'count', 'status != "10"');
        $url = base_url() . 'patient/list_pat/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('patients', $this->start_no, $this->per_page, 'date DESC', $group_by = false, '', 'status !="10" ');

        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function list_service($page = false) {
        /*         * pagination */
        $this->per_page = 20;
        $row = $this->general_model->get('patients', '', '', '', '', 'count', 'patients.date', $group_by = false, '', 'patients.status !="10" and patients.pat_id not in (select pat_id from check_pat where check_status != "ประเมิณเรียบร้อยแล้ว")');
        $url = base_url() . 'patient/list_service/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('patients', $this->start_no, $this->per_page, 'patients.date', $group_by = false, '', 'patients.status !="10" and patients.pat_id not in (select pat_id from check_pat where check_status != "ประเมิณเรียบร้อยแล้ว") ');

        // echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function list_teach($page = false) {
        /*         * pagination */
        $this->per_page = 20;
        $row = $this->general_model->get('check_pat', '', '', '', '', 'count', 'check_pat.check_status = "กำลังจัดทำอุปกรณ์"', 'patients', 'patients.pat_id = check_pat.pat_id');
        $url = base_url() . 'patient/list_teach/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('check_pat', $this->start_no, $this->per_page, 'check_pat.date DESC', $group_by = false, '', 'check_pat.check_status = "กำลังจัดทำอุปกรณ์" ', 'patients', 'patients.pat_id = check_pat.pat_id');

        // echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function list_pat_del($page = false) {
        /*         * pagination */
        $this->per_page = 20;
        $row = $this->general_model->get('patients', '', '', '', '', 'count', 'status = "10"');
        $url = base_url() . 'patient/index/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('patients', $this->start_no, $this->per_page, 'date DESC', $group_by = false, '', 'status ="10" ');

        // echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function list_pat_check($page = false) {
        $check_status = ' "ประเมิณเรียบร้อยแล้ว"';

        /*         * pagination */
        $this->per_page = 1;
        $row = $this->general_model->get('check_pat', '', '', '', '', 'count', "check_status not in ({$check_status})", 'patients', 'patients.pat_id = check_pat.pat_id');
        $url = base_url() . 'patient/list_pat_check/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('check_pat', $this->start_no, $this->per_page, 'check_pat.date DESC', $group_by = false, '', "check_status not in ({$check_status})", 'patients', 'patients.pat_id = check_pat.pat_id');

        // echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function list_pat_checked($page = false) {
        /*         * pagination */
        $this->per_page = 20;
        $row = $this->general_model->get('check_pat', '', '', '', '', 'count', 'check_status = "ประเมิณเรียบร้อยแล้ว"', 'patients', 'patients.pat_id = check_pat.pat_id');
        $url = base_url() . 'patient/index/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('check_pat', $this->start_no, $this->per_page, 'check_pat.date DESC', $group_by = false, '', 'check_status = "ประเมิณเรียบร้อยแล้ว"', 'patients', 'patients.pat_id = check_pat.pat_id');

        // echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function list_owner_doc($page = false) {
        // $check_status = ' "รอตรวจก่อนทำสวยงาม","รอตรวจหลังทำสวยงาม" ';
        /*         * pagination */
        $this->per_page = 1;
        $row = $this->general_model->get('check_pat', '', '', '', '', 'count', "check_status != 'ประเมิณเรียบร้อยแล้ว' and (doc_id = {$this->user_id} or doc_id_before = {$this->user_id}  or doc_id_after = {$this->user_id} )", 'patients', 'patients.pat_id = check_pat.pat_id');
        $url = base_url() . 'patient/index/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('check_pat', $this->start_no, $this->per_page, 'check_pat.date DESC', $group_by = false, '', "check_status != 'ประเมิณเรียบร้อยแล้ว' and (doc_id = {$this->user_id} or doc_id_before = {$this->user_id}  or doc_id_after = {$this->user_id} )", 'patients', 'patients.pat_id = check_pat.pat_id');

        // echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/list_pat', $data);
    }

    public function del($id) {

        $data = array('status' => 10,);
        $this->general_model->update('patients', $data, "pat_id = {$id} ");
        $ref = $this->input->server('HTTP_REFERER', TRUE);
        redirect($ref, 'location');
    }

    public function return_del($id) {

        $data = array('status' => 1,);
        $this->general_model->update('patients', $data, "pat_id = {$id} ");
        $ref = $this->input->server('HTTP_REFERER', TRUE);
        redirect($ref, 'location');
    }

    public function edit_profile($id) {


        $data['pat_nationality'] = $this->general_model->get_field('distinct(pat_nationality)', 'patients');
        $data['pat_race'] = $this->general_model->get_field('distinct(pat_race)', 'patients');
        $data['pat_raligion'] = $this->general_model->get_field('distinct(pat_raligion)', 'patients');
        $data['pat_adr1'] = $this->general_model->get_row('adress', "pat_id = {$id} and ads_type ='1'");
        $data['pat_adr2'] = $this->general_model->get_row('adress', "pat_id = {$id} and ads_type ='2'");

        $post = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['patient'] = $post;
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/edit_profile', $data);
    }

    public function query_profile($id) {


        $check_null = array('road', 'street', 'c_road', 'c_treet', 'pat_tel_local', 'pat_mobile_local', 'pat_tel_current', 'pat_mobile_current');
        foreach ($check_null as $key => $value) {

            if ($this->input->post($value) == '') {
                $_POST[$value] = " - ";
            }
        }
        $param = array
            (
            'place' => $this->input->post('place'),
            'province' => $this->input->post('province'),
            'date' => $this->input->post('date'),
            'pat_right' => $this->input->post('pat_right'),
            'pat_name' => $this->input->post('pat_name'),
            'pat_lname' => $this->input->post('pat_lname'),
            'pat_birth' => $this->input->post('pat_birth'),
            'pat_year' => $this->input->post('pat_year'),
            'pat_sex' => $this->input->post('pat_sex'),
            'pat_status' => $this->input->post('pat_status'),
            'pat_nationality' => $this->input->post('pat_nationality'),
            'pat_race' => $this->input->post('pat_race'),
            'pat_raligion' => $this->input->post('pat_raligion'),
            'pat_id_card' => $this->input->post('pat_id_card'),
            'pat_address_local' => '<b>เลขที่</b>..... ' . $this->input->post('no') . ' .....<b>หมู่ที่</b>..... ' . $this->input->post('moo') . '   .....<b>ชื่อหมูบ้าน</b>..... ' . $this->input->post('house') . ' .....<b>ถนน</b>..... ' . $this->input->post('road') . '  .....<b>ซอย</b>..... ' . $this->input->post('street') . ' .....<br><b>ตำบล</b>..... ' . $this->input->post('tombol') . ' .....<b>อำเภอ</b>..... ' . $this->input->post('aumper') . ' <br>.....<b>จังหวัด</b>..... ' . $this->input->post('j_province') . ' .....<b>รหัสไปรษณีย์</b>..... ' . $this->input->post('zipcode') . '  .....<b>ประเทศ</b>.....  ' . $this->input->post('country'),
            'pat_tel_local' => $this->input->post('pat_tel_local'),
            'pat_mobile_local' => $this->input->post('pat_mobile_local'),
            'pat_address_current' => '<b>เลขที่</b>.....' . $this->input->post('c_no') . ' .....<b>หมู่ที่</b>..... ' . $this->input->post('c_moo') . '   .....<b>ชื่อหมูบ้าน</b>.....  ' . $this->input->post('c_house') . ' .....<b>ถนน</b>.....  ' . $this->input->post('c_road') . '  .....<b>ซอย</b>.....  ' . $this->input->post('c_street') . ' .....<br><b>ตำบล</b>.....  ' . $this->input->post('c_tombol') . ' .....<b>อำเภอ</b>.....  ' . $this->input->post('c_aumper') . '<br> .....<b>จังหวัด</b>.....  ' . $this->input->post('c__province') . ' .....<b>รหัสไปรษณีย์</b>.....  ' . $this->input->post('c_zipcode') . '  .....<b>ประเทศ</b>.....  ' . $this->input->post('c_country'),
            'pat_tel_current' => $this->input->post('pat_tel_current'),
            'pat_mobile_current' => $this->input->post('pat_mobile_current'),
            'pat_contact_name' => $this->input->post('pat_contact_name'),
            'pat_contact_lname' => $this->input->post('pat_contact_lname'),
            'pat_contact_rel' => $this->input->post('pat_contact_rel'),
            'pat_contact_tel' => $this->input->post('pat_contact_tel'),
            'pat_contact_mobile' => $this->input->post('pat_contact_mobile'),
            'pat_job_before' => $this->input->post('pat_job_before'),
            'pat_job_current' => $this->input->post('pat_job_current'),
            'pat_cause' => $this->input->post('pat_cause'),
            'pat_date_amp_left' => $this->input->post('pat_date_amp_left'),
            'pat_date_amp_right' => $this->input->post('pat_date_amp_right'),
            'amp_type_other' => $this->input->post('amp_type_other'),
            'pat_pros_place_radio' => $this->input->post('pat_pros_place_radio'),
            'pat_pros_place' => $this->input->post('pat_pros_place'),
            'pat_pros_date' => $this->input->post('pat_pros_date'),
            'pat_move_ment' => $this->input->post('pat_move_ment'),
            'pat_move_ment_tool' => $this->input->post('pat_move_ment_tool'),
        );

        if ($this->input->post('skip') == 1) {
            unset($param['pat_address_current']);
            $param['pat_address_current'] = $param['pat_address_local'];
        }




        $this->general_model->update('patients', $param, "pat_id = {$id}");
        $pat_id = $id;
        $data_adr_local = array
            (
            'pat_id' => $pat_id,
            'no' => $this->input->post('no'),
            'moo' => $this->input->post('moo'),
            'house' => $this->input->post('house'),
            'road' => $this->input->post('road'),
            'street' => $this->input->post('street'),
            'tombol' => $this->input->post('tombol'),
            'aumper' => $this->input->post('aumper'),
            'province' => $this->input->post('j_province'),
            'zipcode' => $this->input->post('zipcode'),
            'country' => $this->input->post('c_country'),
            'ads_type' => 1,
        );
        $data_adr_current = array
            (
            'pat_id' => $pat_id,
            'no' => $this->input->post('c_no'),
            'moo' => $this->input->post('c_moo'),
            'house' => $this->input->post('c_house'),
            'road' => $this->input->post('c_road'),
            'street' => $this->input->post('c_street'),
            'tombol' => $this->input->post('c_tombol'),
            'aumper' => $this->input->post('c_aumper'),
            'province' => $this->input->post('c_province'),
            'zipcode' => $this->input->post('c_zipcode'),
            'country' => $this->input->post('c_country'),
            'ads_type' => 2,
        );

        if ($this->input->post('skip') == 1) {
            $data_adr_current = $data_adr_local;
            $data_adr_current['ads_type'] = 2;
        }
        $ads_id1 = $this->general_model->get_row_select('ads_id', 'adress', "pat_id = {$id} and ads_type ='1'");
        $ads_id2 = $this->general_model->get_row_select('ads_id', 'adress', "pat_id = {$id} and ads_type ='2'");
        $this->general_model->update('adress', $data_adr_local, "pat_id = {$id} and ads_id = {$ads_id1['ads_id']}");
        $this->general_model->update('adress', $data_adr_current, "pat_id = {$id} and ads_id = {$ads_id2['ads_id']}");

        redirect(base_url() . 'patient/profile/' . $id);
    }

    public function history($id) {

        $data['patient'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'] = $this->general_model->get_all('check_pat', "pat_id = {$id} ",'','date DESC , check_id DESC'); ##
        $data['evol'] = $this->general_model->get_all('evolution', "pat_id = {$id} "); ##
        //echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('pat/history', $data);
    }

    function check_pat($id, $type = false) {##ตรวจ
        if ($id == false) {
            $ref = $this->input->server('HTTP_REFERER', TRUE);
            redirect($ref, 'location');
        }
        $data['check'][] = $this->general_model->get_row('check_pat', "check_id = {$id} "); ##
        // var_dump($data['check']);die();

        if ($data['check'][0] == false) {
            $ref = $this->input->server('HTTP_REFERER', TRUE);
            ?>
            <meta charset="utf-8">
            <script type="text/javascript">
                var url = '<?php echo $ref; ?>';
                if (confirm("ยังไม่ได้รับการตรวจ ในบริการนี้ !") == true) {
                    window.location.href = url;
                } else {
                    window.location.href = url;
                }

            </script>

            <?php
        }

        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$data['check'][0]['pat_id']}");
        $data['evol'][] = $this->general_model->get_row('evolution', "pat_id = {$id} and check_id = {$data['check'][0]['check_id']}"); ##ดึงเฉพาะล่าสุด
        if ($type == 'check_pat_before') {
            parent::core_doctor_view('pat/check_pat_before', $data);
        } elseif ($type == 'check_pat_after') {
            parent::core_doctor_view('pat/check_pat_after', $data);
        } elseif ($type == 'evol') {
            parent::core_doctor_view('pat/evol', $data);
        } else {
            parent::core_doctor_view('pat/check_pat', $data);
        }
    }

    public function export_card($id) {

        $data['patient'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'] = $this->general_model->get_all('check_pat', "pat_id = {$id} ",'','date ASC,check_id ASC'); ##
        //echo $this->db->last_query();die();
        //
       

        
        $file_name = 'บัตรประจำตัวผุ้ป่วย ' . $data['patient']['pat_name'] . ' ' . $data['patient']['pat_lname'];
        ob_start();
        $arr_data = array();
        $arr_data_tmp = array();
        ?> 
        <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>asset/css/style.css">
        <meta charset="utf-8">
        <style>
            body
        {
            background-color: white;
        }
        
        </style>
        
        <div style="text-align: center;">

                 <div style="width: 49.8%;height:100%;float: left;border-right:1px solid black;text-align: center;vertical-align: middle;">
                    
                    <div style="height: 50%;border-bottom: 1px solid black;padding-top: 40px;">
                         <img src="<?php echo base_url() ?>img/logo.png" width="80px" height="80px" style="margin-top: 1px;">
                         <h2>บัตรประจำตัวผู้พิการ</h2>
                        <h4>เลขที่ F.N. ........  <?php echo $data['patient']['pat_id']; ?> ........... </h4>
                        <h4>ชื่อ ..... <?php echo $data['patient']['pat_name']; ?> .....  นามสกุล .....<?php echo  $data['patient']['pat_lname'];?> .....</h4>
                        <h4>มารับการตรวจครั้งแรกวันที่ ..... <?php echo _set_date($data['patient']['date']); ?> ..... </h4>
                        <h4>
                            บริการทำขาเทียมระดับ ..... 
                            <?php echo _get_AmpTypeName($data['patient']['amp_type_id']); ?>
                           <?php echo ($data['patient']['amp_type_id'] == '' ? 'อื่นๆ  คือ ' . $data['patient']['amp_type_id'] : '') ?>
                            ..... 
                        </h4>
                    </div>

                    <div style="height: 45%;" style="vertical-align: middle;">
                        <h2><b>มูลนิธิขาเทียม<b> ในสมเด็จพระศรีนครินทราบรมราชชนนี</h2>
                        <h4>199 หมู่ 4 ตำบลดอนแก้ว อำเภอแม่ริม  จังหวัดเชียงใหม่ 50180</h4>
                         <h4>โทร. 0-5311-2271-3  โทรสาร. : 0-5311-2275</h4>
                         ______________________________________
                         <h4>บริการตรวจ เวลา 8.00 - 10.30 น.</h4>
                         <h4>เวลาทำการ จันทร์ - ศุกร์ เวลา .00 - 16.30 น.</h4>
                         <h4> เว้นวันเสาร์ - อาทิตย์ และวันหยุดนักขัตฤกษ์</h4>
                         <br>
                         <h4>"เพื่อประโยชน์ของท่าน กรุณานำบัตรนี้มาแสดงทุกครั้งที่มาติดต่อรับบริการ"</h4>
                    </div>
                    

                </div>
                
                 <div style="width: 49.8%;height:100%;float: left;border-left:1px solid black;text-align: center;vertical-align: middle;">
                  <h2 style="margin-top: 25px">การรับบริการ</h2>
                  <style>
                    table {
                        border-collapse: collapse;
                    }

                    table, td, th {
                        border: 1px solid black;
                    }
                    td{
                        line-height: 1.5;
                    }
                   
               </style>
                  <table  style="width: 90%;margin: 0 auto;">
                      <tr >
                          <th>วันเดือนปี</th>
                          <th>รับบริการ</th>
                          <th>ผู้ให้บริการ</th>
                          <th>หมายเหตุ</th>
                      </tr>
                      <?php 
                      $count = count($data['check']);
                      foreach ($data['check'] as $key => $value) 
                      {
                          ?>
                        <tr>
                          <td class="center"><?php echo _set_date($value['date']); ?></td>
                          <td class="center">ทำขาเทียม</td>
                          <td class="center"><?php echo $this->get_value->get_staff_name($value['sf_id']); ?></td>
                          <td class="center"><?php echo $value['comment']; ?></td>
                        </tr>
                          <?php
                      }
                      $i = 1;
                      $set = 32 - $count;
                      while ( $i <= $set) {
                          ?>
                           <tr>
                          <td class="center">&nbsp; </td>
                          <td class="center"> </td>
                          <td class="center"> </td>
                          <td class="center"> </td>
                        </tr>
                          <?php
                          $i++;
                      }

                        ?>
                  </table>
                      
                      

                </div>
            </div>
        <?php
        $html = ob_get_contents();
        ob_end_clean();

        $this->mpdf = new mPDF('th', 'A4-L', '0','','0px','0px','0px','0px','0px','0px'); // mode - default ''
        $this->mpdf->WriteHTML($html); // สั่งให้ mPDF เขียนไฟล์ pdf
        $this->mpdf->Output($file_name . '.pdf', 'D'); // จากนั้นส่งชื่อไฟล์ออกมาครับผม            
        // $view = 'core_' . $this->usertype . '_view';
        // parent::$view('pat/history', $data);
    }
     public function export_only_profile($id) {

        $data['patient'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['pat_adr1'] = $this->general_model->get_row('adress', "pat_id = {$id} and ads_type ='1'");
        $data['pat_adr2'] = $this->general_model->get_row('adress', "pat_id = {$id} and ads_type ='2'");
        //echo $this->db->last_query();die();
        //
       

        
        $file_name = 'ประวัติผู้ป่วย ' . $data['patient']['pat_name'] . ' ' . $data['patient']['pat_lname'];
        ob_start();
        $arr_data = array();
        $arr_data_tmp = array();
        ?> 
        <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>asset/css/style.css">
        <meta charset="utf-8">
        <style>
            body
        {
            background-color: white;
        }
        span
        {
            line-height: 3;
        }
        .no{
            margin-top: 10px;
            margin-bottom: 8px;
            text-align: justify;
            font-size: 15px;
        }
        .sub_no
        {
            margin-top: 8px;
            text-align: justify;
            font-size: 15px;
        }   
        
        </style>
        
        <div style="">
                <div style="">
                <div style="text-align: center;font-size: 15px;">
                    <img src="<?php echo base_url() ?>img/logo.png" width="90px" height="90px" style=""><br>
                    <span><b>มูลนิธิขาเทียมในสมเด็จพระศรีนครินทราบรมราชชนนี  Prostheses Foundation of H.R.H. the Princess Mother</b></span><br>
                    <span><u><b>แบบประวัติผู้ใช้บริการทำขาเทียม Record of amputee in prosthetic service</b></u></span><br>
                    _____________________________________________________________________________________
                </div>
                <div class="clear"></div>
                <div class="no" style="">
                    <span><u><b>ส่วนที่ 1 - ประวัติคนพิการ Part 1 - General Information</b></u></span><br>
                    <span >1.1 ชื่อ Name .....<?php echo $data['patient']['pat_name'] ?>.....</span>
                    <span>นามสกุล Last name .....<?php echo $data['patient']['pat_lname'] ?>.....</span>
                    <span>วัน/เดือน/ปีเกิด Date of Birth .....<?php echo _set_date($data['patient']['pat_birth']) ?>.....</span>

                </div>
                 <div class="sub_no"style="">&nbsp;&nbsp;&nbsp;
                    <span>อายุ Age .....<?php echo $data['patient']['pat_year'] ?>..... ปี Years</span>
                    <span>สัญชาติ Citizenship .....<?php echo $data['patient']['pat_nationality'] ?>.....</span>
                    <span>เชื้อชาติ Race .....<?php echo $data['patient']['pat_race'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.2 เพศ Sex .....<?php echo $data['patient']['pat_sex'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.3 เลขที่บัตรประชาชน/บัตรต่างด้าว Identification card number  .....<?php echo $data['patient']['pat_id_card'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.4 สถานภาพ Status  .....<?php echo $data['patient']['pat_status'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.5 ศาสนา Religion   .....<?php echo $data['patient']['pat_raligion'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.6 ที่อยู่ตามบัตรประชาชน/ทะเบียนบ้าน Address in your Identification card /House Registration </span><br>&nbsp;&nbsp;&nbsp;
                    <span>เลขที่ number   .....<?php echo $data['pat_adr1']['no'] ?>.....</span>
                    <span>หมู่ที่    .....<?php echo $data['pat_adr1']['moo'] ?>.....</span>
                    <span>หมู่บ้าน Village   .....<?php echo $data['pat_adr1']['house'] ?>.....</span>
                    <span>ถนน Road   .....<?php echo $data['pat_adr1']['road'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>ซอย Street .....<?php echo $data['pat_adr1']['street'] ?>..... </span>
                    <span>ตำบล Subdistrict .....<?php echo $data['pat_adr1']['tombol'] ?>.....</span>
                    <span>อำเภอ District .....<?php echo $data['pat_adr1']['aumper'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>จังหวัด Province/State .....<?php echo $data['pat_adr1']['province'] ?>..... </span>
                    <span>รหัสไปรษณีย์ Zip code .....<?php echo $data['pat_adr1']['zipcode'] ?>.....</span>
                    <span>ประเทศ Country .....<?php echo $data['pat_adr1']['country'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>โทรศัพท์บ้าน Telephone .....<?php echo $data['patient']['pat_tel_local'] ?>..... </span>
                    <span>โทรศัพท์มืถือ Mobile phone .....<?php echo $data['patient']['pat_mobile_local'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.7 ที่อยู่ปัจจุบัน/ Current Address  </span><br>&nbsp;&nbsp;&nbsp;
                    <span>เลขที่ number   .....<?php echo $data['pat_adr2']['no'] ?>.....</span>
                    <span>หมู่ที่    .....<?php echo $data['pat_adr2']['moo'] ?>.....</span>
                    <span>หมู่บ้าน Village   .....<?php echo $data['pat_adr2']['house'] ?>.....</span>
                    <span>ถนน Road   .....<?php echo $data['pat_adr2']['road'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>ซอย Street .....<?php echo $data['pat_adr2']['street'] ?>..... </span>
                    <span>ตำบล Subdistrict .....<?php echo $data['pat_adr2']['tombol'] ?>.....</span>
                    <span>อำเภอ District .....<?php echo $data['pat_adr2']['aumper'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>จังหวัด Province/State .....<?php echo $data['pat_adr2']['province'] ?>..... </span>
                    <span>รหัสไปรษณีย์ Zip code .....<?php echo $data['pat_adr2']['zipcode'] ?>.....</span>
                    <span>ประเทศ Country .....<?php echo $data['pat_adr2']['country'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>โทรศัพท์บ้าน Telephone .....<?php echo $data['patient']['pat_tel_current'] ?>..... </span>
                    <span>โทรศัพท์มืถือ Mobile phone .....<?php echo $data['patient']['pat_mobile_current'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.8 ชื่อผู้ที่สามารถติดต่อได้ Name of the person can be contacted with </span><br>&nbsp;&nbsp;&nbsp;
                    <span>ชื่อ Name   .....<?php echo $data['patient']['pat_contact_name'] ?>.....</span>
                    <span>นามสกุล Last name   .....<?php echo $data['patient']['pat_contact_lname'] ?>.....</span>
                    <span>ความเกี่ยวข้อง Relationship .....<?php echo $data['patient']['pat_contact_rel'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>โทรศัพท์บ้าน Telephone   .....<?php echo $data['patient']['pat_contact_tel'] ?>.....</span>
                    <span>โทรศัพท์มือถือ Mobile   .....<?php echo $data['patient']['pat_contact_mobile'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.9 อาชีพก่อนถูกตัดขา Occupation before being ampuated .....<?php echo $data['patient']['pat_job_before'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.10 อาชีพปัจจุบัน Current Occupation .....<?php echo $data['patient']['pat_job_current'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.11 สาเหตุที่ถูกตัดขา Cause of amputation .....<?php echo $data['patient']['pat_cause'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.12 วันที่ถูกตัดขา Date of amputation </span><br>&nbsp;&nbsp;&nbsp;
                    <span>ขาซ้าย เมื่อ Left side   .....<?php echo _set_date($data['patient']['pat_date_amp_left']) ?>.....</span><br>&nbsp;&nbsp;&nbsp;
                    <span>ขาขวา เมื่อ Right side   .....<?php echo _set_date($data['patient']['pat_date_amp_right']) ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>
                    1.13 ชนิดของขาที่ถูกตัด Amputation's type .....
                    <?php echo _get_AmpTypeName($data['patient']['amp_type_id']); ?>
                    <?php echo ($data['patient']['amp_type_id'] == '' ? 'อื่นๆ  คือ ' . $data['patient']['amp_type_id'] : '') ?>
                    
                    .....
                    </span>
                </div>
                <div class="no" style="">
                    <span>
                    1.14 การใส่ขาเทียม About prostheses .....
                    <?php echo $data['patient']['pat_pros_place_radio'] ?>
                    <?php echo ($data['patient']['pat_pros_place'] != '' ? ' ..... จาก ...... ' . $data['patient']['pat_pros_place'] : '') ?>.....<br>&nbsp;&nbsp;&nbsp;
                    <?php echo ($data['patient']['pat_pros_date'] != '' ? '  เมื่อ ...... ' . _set_date_full($data['patient']['pat_pros_date']) : '') ?>
                    .....
                    </span>
                </div>
                <div class="no" style="">
                    <span>
                    1.15 การเคลือนที่ของคนพิการ Ambulation.....
                    <?php echo $data['patient']['pat_move_ment'] ?>
                    <?php echo ($data['patient']['pat_move_ment_tool'] != '' ? ' ..... คือ ...... ' . $data['patient']['pat_move_ment_tool'] : '') ?>
                    .....
                    </span>
                </div>

                </div>
            </div>
        <?php
        // die();
        $html = ob_get_contents();
        ob_end_clean();

        $this->mpdf = new mPDF('th', 'A4', '0'); // mode - default ''
        $this->mpdf->WriteHTML($html); // สั่งให้ mPDF เขียนไฟล์ pdf
        $this->mpdf->Output($file_name . '.pdf', 'D'); // จากนั้นส่งชื่อไฟล์ออกมาครับผม            
        // $view = 'core_' . $this->usertype . '_view';
        // parent::$view('pat/history', $data);
    }

public function export_checkpat($check_id) {
         if ($check_id == false) {
                $ref = $this->input->server('HTTP_REFERER', TRUE);
                redirect($ref, 'location');
            }
        $data['check'] = $this->general_model->get_row('check_pat', "check_id = {$check_id} "); ##
        $check_order = $this->general_model->get_field('check_id','check_pat',"pat_id = {$data['check']['pat_id']}",'date ASC,check_id ASC');
        $i=1;
        foreach ($check_order as $key => $value) 
        {
            if($value['check_id'] == $check_id)
            {
                $step = $i;##ลำดับ
            }
            $i++;
        }

        $data['patient'] = $this->general_model->get_row('patients', "pat_id = {$data['check']['pat_id']}");
        $data['evol'] = $this->general_model->get_row('evolution', "pat_id = {$data['check']['pat_id']} and check_id = {$data['check']['check_id']}"); ##ดึงเฉพาะล่าสุด
        $data['tool'][] = $this->general_model->get_row('order_tools', "check_id = {$data['check']['check_id']}"); ##ดึงเฉพาะล่าสุด

        $data['pat_adr1'] = $this->general_model->get_row('adress', "pat_id = {$data['check']['pat_id']} and ads_type ='1'");
        $data['pat_adr2'] = $this->general_model->get_row('adress', "pat_id = {$data['check']['pat_id']} and ads_type ='2'");
        //echo $this->db->last_query();die();
        //
       

        
        $file_name = 'ใบรายการตรวจ ครั้งที่  '.$step.' ' . $data['patient']['pat_name'] . ' ' . $data['patient']['pat_lname'];
        ob_start();
        $arr_data = array();
        $arr_data_tmp = array();
        ?> 
        <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>asset/css/style.css">
        <meta charset="utf-8">
        <style>
            body
        {
            background-color: white;
        }
        span
        {
            line-height: 3;
        }
        .no{
            margin-top: 10px;
            margin-bottom: 8px;
            text-align: justify;
            font-size: 15px;
        }
        .sub_no
        {
            margin-top: 8px;
            text-align: justify;
            font-size: 15px;
        }   
        
        </style>
        
        <div style="">
                <div style="">
                <div style="text-align: center;font-size: 15px;">
                    <img src="<?php echo base_url() ?>img/logo.png" width="90px" height="90px" style=""><br>
                    <span><b>มูลนิธิขาเทียมในสมเด็จพระศรีนครินทราบรมราชชนนี  Prostheses Foundation of H.R.H. the Princess Mother</b></span><br>
                    <span><u><b>แบบประวัติผู้ใช้บริการทำขาเทียม Record of amputee in prosthetic service</b></u></span><br>
                    <span><b>ครั้งที่ Number of mobile unit ..... <?php echo $step; ?> .....</b></span>&nbsp;&nbsp;
                    <span><b>วันที่ Date .....<?php  echo _set_date_full($data['check']['date'])?></b></span><br>
                    <span><b>ณ  At .....<?php  echo $data['check']['place']?>.....</b></span>&nbsp;&nbsp;
                    <span><b>จังหวัด Province / State ..... <?php  echo $data['check']['province']?> ..... </b></span>
                    _____________________________________________________________________________________
                </div>
                <div class="clear"></div>
                <div class="no" style="">
                    <span><u><b>ส่วนที่ 1 - ประวัติคนพิการ Part 1 - General Information</b></u></span><br>
                    <span >1.1 ชื่อ Name .....<?php echo $data['patient']['pat_name'] ?>.....</span>
                    <span>นามสกุล Last name .....<?php echo $data['patient']['pat_lname'] ?>.....</span>
                    <span>วัน/เดือน/ปีเกิด Date of Birth .....<?php echo _set_date($data['patient']['pat_birth']) ?>.....</span>

                </div>
                 <div class="sub_no"style="">&nbsp;&nbsp;&nbsp;
                    <span>อายุ Age .....<?php echo $data['patient']['pat_year'] ?>..... ปี Years</span>
                    <span>สัญชาติ Citizenship .....<?php echo $data['patient']['pat_nationality'] ?>.....</span>
                    <span>เชื้อชาติ Race .....<?php echo $data['patient']['pat_race'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.2 เพศ Sex .....<?php echo $data['patient']['pat_sex'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.3 เลขที่บัตรประชาชน/บัตรต่างด้าว Identification card number  .....<?php echo $data['patient']['pat_id_card'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.4 สถานภาพ Status  .....<?php echo $data['patient']['pat_status'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.5 ศาสนา Religion   .....<?php echo $data['patient']['pat_raligion'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.6 ที่อยู่ตามบัตรประชาชน/ทะเบียนบ้าน Address in your Identification card /House Registration </span><br>&nbsp;&nbsp;&nbsp;
                    <span>เลขที่ number   .....<?php echo $data['pat_adr1']['no'] ?>.....</span>
                    <span>หมู่ที่    .....<?php echo $data['pat_adr1']['moo'] ?>.....</span>
                    <span>หมู่บ้าน Village   .....<?php echo $data['pat_adr1']['house'] ?>.....</span>
                    <span>ถนน Road   .....<?php echo $data['pat_adr1']['road'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>ซอย Street .....<?php echo $data['pat_adr1']['street'] ?>..... </span>
                    <span>ตำบล Subdistrict .....<?php echo $data['pat_adr1']['tombol'] ?>.....</span>
                    <span>อำเภอ District .....<?php echo $data['pat_adr1']['aumper'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>จังหวัด Province/State .....<?php echo $data['pat_adr1']['province'] ?>..... </span>
                    <span>รหัสไปรษณีย์ Zip code .....<?php echo $data['pat_adr1']['zipcode'] ?>.....</span>
                    <span>ประเทศ Country .....<?php echo $data['pat_adr1']['country'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>โทรศัพท์บ้าน Telephone .....<?php echo $data['patient']['pat_tel_local'] ?>..... </span>
                    <span>โทรศัพท์มืถือ Mobile phone .....<?php echo $data['patient']['pat_mobile_local'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.7 ที่อยู่ปัจจุบัน/ Current Address  </span><br>&nbsp;&nbsp;&nbsp;
                    <span>เลขที่ number   .....<?php echo $data['pat_adr2']['no'] ?>.....</span>
                    <span>หมู่ที่    .....<?php echo $data['pat_adr2']['moo'] ?>.....</span>
                    <span>หมู่บ้าน Village   .....<?php echo $data['pat_adr2']['house'] ?>.....</span>
                    <span>ถนน Road   .....<?php echo $data['pat_adr2']['road'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>ซอย Street .....<?php echo $data['pat_adr2']['street'] ?>..... </span>
                    <span>ตำบล Subdistrict .....<?php echo $data['pat_adr2']['tombol'] ?>.....</span>
                    <span>อำเภอ District .....<?php echo $data['pat_adr2']['aumper'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>จังหวัด Province/State .....<?php echo $data['pat_adr2']['province'] ?>..... </span>
                    <span>รหัสไปรษณีย์ Zip code .....<?php echo $data['pat_adr2']['zipcode'] ?>.....</span>
                    <span>ประเทศ Country .....<?php echo $data['pat_adr2']['country'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>โทรศัพท์บ้าน Telephone .....<?php echo $data['patient']['pat_tel_current'] ?>..... </span>
                    <span>โทรศัพท์มืถือ Mobile phone .....<?php echo $data['patient']['pat_mobile_current'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.8 ชื่อผู้ที่สามารถติดต่อได้ Name of the person can be contacted with </span><br>&nbsp;&nbsp;&nbsp;
                    <span>ชื่อ Name   .....<?php echo $data['patient']['pat_contact_name'] ?>.....</span>
                    <span>นามสกุล Last name   .....<?php echo $data['patient']['pat_contact_lname'] ?>.....</span>
                    <span>ความเกี่ยวข้อง Relationship .....<?php echo $data['patient']['pat_contact_rel'] ?>.....</span>
                </div>
                <div class="sub_no" style="">&nbsp;&nbsp;&nbsp;
                    <span>โทรศัพท์บ้าน Telephone   .....<?php echo $data['patient']['pat_contact_tel'] ?>.....</span>
                    <span>โทรศัพท์มือถือ Mobile   .....<?php echo $data['patient']['pat_contact_mobile'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.9 อาชีพก่อนถูกตัดขา Occupation before being ampuated .....<?php echo $data['patient']['pat_job_before'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.10 อาชีพปัจจุบัน Current Occupation .....<?php echo $data['patient']['pat_job_current'] ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>1.11 สาเหตุที่ถูกตัดขา Cause of amputation .....<?php echo $data['patient']['pat_cause'] ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>1.12 วันที่ถูกตัดขา Date of amputation </span><br>&nbsp;&nbsp;&nbsp;
                    <span>ขาซ้าย เมื่อ Left side   .....<?php echo _set_date($data['patient']['pat_date_amp_left']) ?>.....</span><br>&nbsp;&nbsp;&nbsp;
                    <span>ขาขวา เมื่อ Right side   .....<?php echo _set_date($data['patient']['pat_date_amp_right']) ?>.....</span>
                </div>
                <div class="no" style="">
                    <span>
                    1.13 ชนิดของขาที่ถูกตัด Amputation's type .....
                    <?php echo _get_AmpTypeName($data['patient']['amp_type_id']); ?>
                    <?php echo ($data['patient']['amp_type_id'] == '' ? 'อื่นๆ  คือ ' . $data['patient']['amp_type_id'] : '') ?>
                    
                    .....
                    </span>
                </div>
                <div class="no" style="">
                    <span>
                    1.14 การใส่ขาเทียม About prostheses .....
                    <?php echo $data['patient']['pat_pros_place_radio'] ?>
                    <?php echo ($data['patient']['pat_pros_place'] != '' ? ' ..... จาก ...... ' . $data['patient']['pat_pros_place'] : '') ?>.....<br>&nbsp;&nbsp;&nbsp;
                    <?php echo ($data['patient']['pat_pros_date'] != '' ? '  เมื่อ ...... ' . _set_date_full($data['patient']['pat_pros_date']) : '') ?>
                    .....
                    </span>
                </div>
                <div class="no" style="">
                    <span>
                    1.15 การเคลือนที่ของคนพิการ Ambulation.....
                    <?php echo $data['patient']['pat_move_ment'] ?>
                    <?php echo ($data['patient']['pat_move_ment_tool'] != '' ? ' ..... คือ ...... ' . $data['patient']['pat_move_ment_tool'] : '') ?>
                    .....
                    </span>
                </div>
                <div class="no" style="">
                    <span><u><b>ส่วนที่ 2 - การให้บริการ Part 2 - Detail of serving</b></u></span><br>
                    <span >2.1 แพทย์สั่ง Doctor orders</span><br>&nbsp;&nbsp;&nbsp;
                    <?php
                            $tools = $this->get_value->get_tools_by_check_id($check_id);
                 
                                        if ($tools != false) {
                                            foreach ($tools as $k_tool => $v_tool) {
                                                ?>
                                                <span >
                                                .....
                                                    <?php echo $v_tool['order_type'] . ' : ขา' . $v_tool['neck'] . ' : ' . $this->get_value->get_tools_id($v_tool['tool_id']); ?><?php echo ($v_tool['order_cause'] != '' ? " เหตุผล {$v_tool['order_cause']}" : ''); ?>
                                                .....
                                                </span ><br>
                                                <?php
                                            }
                                        }
                    ?>
                </div>
                 <div class="no" style="">
                    <span>2.2 ชื่อแพทย์ผู้สั่ง Name of Physical check-up doctor   .....<?php echo _doctor_name($data['check']['doc_id']) ?>.....</span>
                </div>
                 <div class="no" style="">
                    <span>2.3 ช่างผู้ทำขาเทียม P&O Technician for making prostheses .....<?php echo _tech_name( $data['tool'][0]['tech_id']) ?>.....</span><br>&nbsp;&nbsp;&nbsp;
                    <span>2.3.1 ชื่อ - นามสกุล Name - Last name .....<?php echo _tech_name( $data['tool'][0]['tech_id']) ?>.....</span>
                </div>
                <div class="no" style="">
                    <span><u><b>ส่วนที่ 3 - การประมิณผลโดยแพทย์ Part 3 - Evaluation and Follow</b></u></span><br>
                    <span >
                        3.1 การประเมิณท่าเดิน Gait Analysis 
                        .....<?php echo $data['evol']['evol_move_result'] ?>.....
                        เหตุผล.....<?php echo $data['evol']['evol_move_result'] ?>.....
                    </span><br>&nbsp;&nbsp;&nbsp;
                    <span style="float:right">ชื่อแพย์ผู้ประเมิณ Doctor's name .....<?php echo _doctor_name($data['evol']['doc_id_move']) ?>.....</span>&nbsp;&nbsp;&nbsp;
                </div>
                <div class="no" style="">
                    <span >
                        3.2 การประเมิณก่อนมอบให้คนพิการ Checkout evaluation
                        .....<?php echo $data['evol']['evol_before_result'] ?>.....
                        เหตุผล.....<?php echo $data['evol']['evol_before_cause'] ?>.....
                    </span><br>&nbsp;&nbsp;&nbsp;
                    <span style="float:right">ชื่อแพย์ผู้ประเมิณ Doctor's name .....<?php echo _doctor_name($data['evol']['doc_id_before']) ?>.....</span>&nbsp;&nbsp;&nbsp;
                </div>



<!-- #### -->
                </div>
            </div>
        <?php
        // die();
        $html = ob_get_contents();
        ob_end_clean();

        $this->mpdf = new mPDF('th', 'A4', '0'); // mode - default ''
        $this->mpdf->WriteHTML($html); // สั่งให้ mPDF เขียนไฟล์ pdf
        $this->mpdf->Output($file_name . '.pdf', 'D'); // จากนั้นส่งชื่อไฟล์ออกมาครับผม            
        // $view = 'core_' . $this->usertype . '_view';
        // parent::$view('pat/history', $data);
    }
######
}
